package com.yb.designpattern.structure.proxy.dongt;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 动态代理类要实现InvocationHandler接口
 */
public class DynamicProxy implements InvocationHandler {

    //要代理的真实对象
    private Object object;

    public DynamicProxy(Object object) {
        this.object = object;
    }

    //重写invoke方法，在执行代理对象的方法前后可以插入自己的逻辑
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("do something before");
        System.out.println("call Method:" + method);
        Object result = method.invoke(object, args);
        System.out.println("result:" + result);
        System.out.println("do something after");
        return result;
    }
}
